from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName

class RegistrationListView(BrowserView):
    """ View for CMB Registration List
    """
    
    def getSchools(self, **kwargs):
        """ Returns all approved registrations
        """
        pc_tool = getToolByName(self, 'portal_catalog')
        query = {
            'portal_type': 'School',
            'review_state': 'published',
            'sort_on': 'sortable_title',
        }
        query.update(kwargs)
        return pc_tool(**query)

    def getResults(self):
        pc_tool = getToolByName(self, 'portal_catalog')
        query = {
            'portal_type': 'Registration',
            'review_state': 'published',
        }
        results = pc_tool(**query)
        registrations = len(results)
        friday_dinner = saturday_lunch = rest = 0
        for r in results:
            o = r.getObject()
            meals=o.getMeals()
            if 'fridayDinner' in meals:
                friday_dinner = friday_dinner + 1
            if 'saturdayLunch' in meals:
                saturday_lunch = saturday_lunch + 1
            if o.getRestArea() == 'yes':
                rest = rest + 1

        return (registrations, friday_dinner, saturday_lunch, rest)
